<template>
	<view class="user_box">
		<view class="header">
			<!-- <view class="flex_ac_js"> -->
				<view class="flex_ac" @tap="goToPage('/pages/users/personalData/personalData')">
					<view class="avatar">
						<image src="@/static/images/user/avatar.svg" mode="aspectFit"></image>
					</view>
					<view class="name">你好，{{userInfo.userName}}同学</view>
				</view>
				<!-- <view class="set" @tap="goToPage()">
					<image src="@/static/images/icon/setup.svg"></image>
				</view> -->
			<!-- </view> -->
		</view>
		
		<view class="vip_box flex_ac">
				<view class="vip_title flex_ac">
					<image src="@/static/images/icon/vip.svg" mode="aspectFit"></image>
				</view>
				<view class="vip_title">
					<view class="title1">升级VIP</view>
					<view class="title2">升级为VIP畅享更多高级功能</view>
				</view>
		</view>
		
		<view class="num_box flex_js">
			<view class="left item_box" @tap="goToPage('/pages/users/points/points')">
				<view class="num_title flex_ac_js">
					<view>积分</view>
					<uni-icons type="right" size="16"></uni-icons>
				</view>
				<view class="flex_ac">
					<image src="@/static/images/icon/points.svg"></image>
					<text style="margin-left: 10rpx;">{{ userInfo.integral }}</text>
				</view>
			</view>
			<view class="right item_box" @click="toPage()">
				<view style="display: flex;align-items: center;justify-content: space-between;">
					<view>
						<view class="">
							<text style="margin-right: 20rpx;">累计学习天数</text>
							<text>{{ userInfo.studyDay}}</text>
						</view>
						<view class="divider"></view>
						<view class="">
							<text style="margin-right: 20rpx;">累计学习单词</text>
							<text>{{ userInfo.studyWords }}</text>
						</view>
					</view>
					<view>
						<uni-icons type="right" :size="26"></uni-icons>
					</view>
				</view>
			</view>
		</view>
		
		<view class="item_box menu_box">
			<view class="flex_ac_js menu_item" v-for="(item, index) in  menuList1" :key="index" @tap="index === 4 ? openPopup() : goToPage(item.menuUrl)">
				<view class="left_title flex_ac">
					<image :src="item.imageItem" mode="aspectFit"></image>
					<text>{{ item.name }}</text>
				</view>
				<view class="flex_ac">
					<text v-if="index === 3" style="color: #858585; font-size: 24rpx;">词汇量: {{chl}}</text>
					<text v-if="index === 4" style="color: #858585; font-size: 24rpx;">{{ readingValue }},{{readingFacility}},{{readingWordNum}}</text>
					<uni-icons type="right" size="16"></uni-icons>
				</view>
			</view>
		</view>
		
		<view class="item_box menu_box" style="margin-top: 80rpx;">
			<view class="flex_ac_js menu_item" v-for="(item, index) in  menuList2" :key="index" @tap="goToPage(item.menuUrl)">
				<view class="left_title flex_ac">
					<image :src="item.imageItem" mode="aspectFit"></image>
					<text>{{ item.name }}</text>
				</view>
				<uni-icons type="right" size="16"></uni-icons>
			</view>
		</view>
		
		<uni-popup ref="popup" type="bottom" background-color="#fff" @maskClick="updateRead">
			<view class="popup_box">
				<view class="popup_title">阅读爱好</view>
			
				<view class="close" @tap="closePopup">
					<uni-icons type="closeempty" size="20" color="#c0bfe6"></uni-icons>
				</view>
				<picker-view  :indicator-style="indicatorStyle"  :value="value" @change="bindChange" class="picker_view">
					<picker-view-column>
						<view class="picker_view_item" v-for="(item,index) in readingArry" :key="index">{{item}}</view>
					</picker-view-column>
					<picker-view-column>
						<view class="picker_view_item" v-for="(item,index) in readingFacilityList" :key="index">{{item}}</view>
					</picker-view-column>
					<picker-view-column>
						<view class="picker_view_item" v-for="(item,index) in readingWordNumList" :key="index">{{item}}词</view>
					</picker-view-column>
				</picker-view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { userInfo,updateReading } from '@/api/user/user.js'
	import config from '@/utils/config.js'
	import imageItem1 from '@/static/images/user/menu_item1.svg'
	import imageItem2 from '@/static/images/user/menu_item2.svg'
	import imageItem3 from '@/static/images/user/menu_item3.svg'
	import imageItem4 from '@/static/images/user/menu_item4.svg'
	import imageItem5 from '@/static/images/user/menu_item5.svg'
	import imageItem6 from '@/static/images/user/menu_item6.svg'
	import imageItem7 from '@/static/images/user/menu_item7.svg'
	import imageItem8 from '@/static/images/user/menu_item8.svg'
	import imageItem9 from '@/static/images/user/menu_item9.svg'
	
	export default {
		data() {
			return {
				chl:0,
				userInfo: {},
				menuList1: [
					{
						imageItem: imageItem1,
						name: '账号',
						menuUrl: '',
					},
					{
						imageItem: imageItem2,
						name: '生词本',
						menuUrl: '/pages/wordList/wordList?type=1',
					},
					{
						imageItem: imageItem3,
						name: '难记词',
						menuUrl: '/pages/wordList/wordList?type=2',
					},
					{
						imageItem: imageItem4,
						name: '词汇量测试',
						menuUrl: '/pages/users/startTesting/startTesting',
					},
					{
						imageItem: imageItem5,
						name: '阅读喜好',
						menuUrl: '',
					},
				],
				menuList2: [
					{
						imageItem: imageItem6,
						name: '服务条款',
						menuUrl: '',
					},
					{
						imageItem: imageItem7,
						name: '隐私条款',
						menuUrl: '',
					},
					{
						imageItem: imageItem8,
						name: '联系我们',
						menuUrl: '',
					},
					{
						imageItem: imageItem9,
						name: '投诉',
						menuUrl: '',
					},
				],
				readingArry: [ '科普','新闻', '故事', '笑话'],
				readingFacilityList:['简单', '中等', '高级'],
				readingWordNumList:[100, 200, 300],
				indicatorStyle: `height: 50px; background-color: #0064fb25;`,
				value: [],
				readingValue: '科普',
				readingFacility:'简单',
				readingWordNum:'100',
			}
		},
		methods: {
			
			
			
			toPage(){
				uni.navigateTo({
					url:"/pages/studyData/studyData"
				})
			},
			goToPage(url) {
				if (url) {
					uni.navigateTo({
						url
					})
				} else {
					uni.showToast({
						title: '功能正在开发...',
						icon:'none'
					})
				}
			},
			openPopup() {
				if(!this.readingValue && !this.readingFacility && !this.readingWordNum) {
					this.value = [0,0,0]
					this.readingValue = '科普'
					this.readingWordNum = 100
					this.readingFacility= '简单'
				}
				this.$refs.popup.open()
			},
			updateRead(){
				if(this.readingValue && this.readingFacility && this.readingWordNum) { 
					let data = {
						readType:this.readingValue,
						readNum:this.readingWordNum,
						readLevel:this.readingFacility
					}
					console.log(data);
					updateReading(data).then(res=>{
						console.log("ddddddddddd",res);
						if(res.code == 200) {
							uni.showToast({
								title:res.msg,
								icon:'success',
								duration:1000
							})
						} else {
							uni.showToast({
								title:res.msg,
								icon:'error',
								duration:1000
							})
						}
					})
				} else {
					uni.showToast({
						title:'请选择阅读喜好',
						icon:'error',
						duration:1000
					})
				}
				
			},
			// 关闭计划弹窗
			closePopup() {
				
				this.$refs.popup.close()
			},
			bindChange(e) {
				
				this.value = e.detail.value
				// this.readingValue = this.readingArry[this.value]
				console.log("ddddddddd",this.value);
				this.readingValue = this.readingArry[this.value[0]]
				this.readingFacility = this.readingFacilityList[this.value[1]]
				this.readingWordNum = this.readingWordNumList[this.value[2]]
				
				console.log(this.readingValue,this.readingFacility,this.readingWordNum);
			},
			async getUserInfo() {
				const { data } = await userInfo()
				console.log("dddddddddddddddddddddddd",data);
				this.userInfo = data.user
				this.readingValue = this.userInfo.readType
				this.readingFacility = this.userInfo.readLevel
				this.readingWordNum = this.userInfo.readNum
				
				this.value = this.getSelectedIndices(this.readingValue, this.readingFacility, this.readingWordNum);
				console.log('xib',this.value);
				uni.setStorageSync('userInfo',this.userInfo)
			},
			
			getSelectedIndices(selectedReading, selectedFacility, selectedWordNum) {
			    const readingIndex = this.readingArry.indexOf(selectedReading);
			    const facilityIndex = this.readingFacilityList.indexOf(selectedFacility);
			    const wordNumIndex = this.readingWordNumList.indexOf(selectedWordNum);
			    return [readingIndex, facilityIndex, wordNumIndex];
			},
		},
		onLoad() {
			this.getUserInfo()
			
			 let chl = uni.getStorageSync("chl")
			 if (chl) {
				 this.chl = chl
			 }
			
			
		},
		onShow() {
			let chl = uni.getStorageSync("chl")
			if (chl) {
				this.chl = chl
			}
		}
	}
</script>

<style lang="scss" scoped>
.user_box {
	height: 100vh;
	padding: 30rpx;
	background: $uni-bg-color-page;
	overflow-y: auto;
	
	.header {
		padding: 130rpx 0 50rpx;
		
		.avatar {
			width: 160rpx;
			height: 160rpx;
			margin-right: 30rpx;
			text-align: center;
			background-color: #e2eefc;
			border-radius: 50%;
			
			image {
				width: 90rpx;
				height: 108rpx;
				border-radius: 50%;
				transform: translateY(26rpx);
			}
		}
		
		.name {
			font-weight: bold;
			font-size: 36rpx;
			color: #092C52;
		}
		
		.set image {
			width: 40rpx;
			height: 40rpx;
		}
		
		.data_box {
			width: 100%;
			margin-top: 50rpx;
			
			.data_item {
				flex: 1;
				text-align: center;
				
				.title1 {
					font-weight: 800;
				}
				
				.title2 {
					margin-top: 16rpx;
					color: $uni-text-color-placeholder;
					font-size: 24rpx;
				}
			}
			// background: linear-gradient(to bottom, red, blue);
		}
	}
	
	.vip_box {
		width: 100%;
		height: 120rpx;
		background-color: #fcf0d5;
		border-radius: 12rpx;
		padding: 10rpx 30rpx;
		margin-bottom: 30rpx;
		
		image {
			width: 50rpx;
			height: 50rpx;
			margin-right: 30rpx;
		}
		
		.vip_title {
			color: #D37171;
			
			.title1 {
				font-weight: 800;
				font-size: 32rpx;
			}
			
			.title2 {
				font-size: 24rpx;
				margin-top: 8rpx;
			}
		}
	}
	
	.item_box {
		background-color: $uni-bg-color;
		border-radius: 12rpx;
		padding: 20rpx 30rpx;
	}
	
	.num_box {
		margin-bottom: 30rpx;
		color: #333;
		
		.left {
			width: 35%;
			
			.num_title {
				margin-bottom: 20rpx;
			}
			
			image {
				width: 36rpx;
				height: 36rpx;
			}
		}
		
		.right {
			width: 60%;
			
			.divider {
				width: 100%;
				height: 2rpx;
				background-color: $uni-border-bottom-color;
				margin: 10rpx 0;
			}
		}
	}
	
	.menu_box {
		color: #333;
		
		.menu_item {
			height: 68rpx;
			border-bottom: 2rpx solid $uni-border-bottom-color;
			
			.left_title {
				image {
					width: 36rpx;
					height: 32rpx;
					margin-right: 20rpx;
				}
			}
		}
	
		.menu_item:last-child {
			border: 0;
		}
	}
	
	.popup_box {
		width: 100%;
		height: 520rpx;
		background-color: #f5f5f5;
		position: relative;
		
		.picker_view {
			height: 100%;
			text-align: center;
			
			.picker_view_item {
				line-height: 50px;
			}
		}
		.popup_title {
			height: 84rpx;
			line-height: 84rpx;
			text-align: center;
		}
		
		.close {
			position: absolute;
			right: 20rpx;
			top: 20rpx;
		}
	}
	// .menu_box {
	// 	margin: 30rpx 10rpx;
	// 	padding: 30rpx 0;
	// 	background-color: #ffffff;
	// 	border-radius: 8rpx;
	// 	display: flex;
	// 	flex-wrap: wrap;
		
	// 	.menu_item {
	// 		width: 25%;
	// 		text-align: center;
	// 		margin-bottom: 30rpx;
			
	// 		image{
	// 			width: 68rpx;
	// 			height: 68rpx;
	// 		}
			
	// 		.menu_text {
	// 			margin-top: 8rpx;
	// 			font-size: 28rpx;
	// 		}
	// 	}
	// }
}
</style>
